public bool isNeedUpdateEvent(TableSchema table)
{

	if(table.Columns.Contains("FEventID") && table.Columns.Contains("FDate")&&table.Columns.Contains("EEventID")&&table.Columns.Contains("EDate"))
	{
		return true;	
	}
	else
	{
		return false;	
	}
}
public bool isEventField(string fieldName)
{
	return string.Equals(fieldName,"FDate",StringComparison.OrdinalIgnoreCase)
			||string.Equals(fieldName,"EDate",StringComparison.OrdinalIgnoreCase)
			||string.Equals(fieldName,"FEventID",StringComparison.OrdinalIgnoreCase)
			||string.Equals(fieldName,"eEventID",StringComparison.OrdinalIgnoreCase);
}
public string GetCNDisplay(ViewSchema SourceView,ViewColumnSchema Column)
{
	string colName = Column.Name.Substring(0,Column.Name.Length-2);
	ViewColumnSchema col = SourceView.Columns[colName];
	if(col.AllowDBNull)
	{
		return "("+GetMemberVariableName(col)+".HasValue)?(("+GetMemberVariableName(col)+".Value)?\"是\":\"否\"):null";
	}
	else
	{
		return "("+GetMemberVariableName(col)+")?\"是\":\"否\"";	
	}
}

public bool isCNDisplayColumn(ViewSchema SourceView, ViewColumnSchema Column)
{
	bool ret= false;	
	if(Column.Name.EndsWith("CN",true,null))
	{
		string colName = Column.Name.Substring(0,Column.Name.Length-2);
		ViewColumnSchema col = SourceView.Columns[colName];
		if(col!=null && col.DataType ==DbType.Boolean)
		{
			ret = true;	
		}
		
	}
	return ret;
}
public string GetColumnDescription(ViewColumnSchema column)
{	
	string description=column.Description;
	if(description=="")
	{
		description=column.Name;
		if(SourceTable.Columns[column.Name]!=null)
		{
			description=SourceTable.Columns[column.Name].Description;
			
		}	
	}
	return (description==null)?"":description.Replace('\n',' ').Replace('\r',' ');
}

public string GetColumnDescription(ColumnSchema column)
{
	string description=column.Description;
	if(description=="")
	{
		description=column.Name;		
	}
	return (description==null)?"":description.Replace('\n',' ').Replace('\r',' ');	
}

public string GetColumnCNNameFromDescription(ColumnSchema column)
{
	string description=GetColumnDescription(column);
	int a=description.IndexOf(":");
	if(a==-1)
	{
		a=description.IndexOf("：");
	}
	if(a!=-1)
	{
		return 	description.Substring(0,a);	
	}
	return description;
}

public string GetColumnCNNameFromDescription(ViewColumnSchema column)
{
	string description=GetColumnDescription(column);
	int a=description.IndexOf(":");
	if(a==-1)
	{
		a=description.IndexOf("：");
	}
	if(a!=-1)
	{
		return 	description.Substring(0,a);	
	}
	return description;
}


public string GetSqlParameterParam(ColumnSchema column)
{
	string param = string.Empty;
	switch (column.DataType)
	{
		case DbType.Decimal:
		{
			param = "(" + column.Precision + ", " + column.Scale + ")";
			break;
		}
		case DbType.AnsiString:
		case DbType.AnsiStringFixedLength:
		case DbType.String:
		case DbType.StringFixedLength:
		case DbType.Binary:
		{
			if (column.NativeType != "text" && column.NativeType != "ntext" && column.NativeType != "image" && column.NativeType != "timestamp")
			{
				if (column.Size > 0)
				{
					param = "(" + column.Size + ")";
				}
				else if (column.Size == -1)
				{
					param = "(MAX)";
				}
			}
			break;
		}
	}	
	return column.NativeType+param+(((bool)column.AllowDBNull)?" AllowDBNull":" NotAllowDBNull");
}

public string GetSqlParameterParam(ViewColumnSchema column)
{
	string param = string.Empty;
	switch (column.DataType)
	{
		case DbType.Decimal:
		{
			param = "(" + column.Precision + ", " + column.Scale + ")";
			break;
		}
		case DbType.AnsiString:
		case DbType.AnsiStringFixedLength:
		case DbType.String:
		case DbType.StringFixedLength:
		case DbType.Binary:
		{
			if (column.NativeType != "text" && column.NativeType != "ntext" && column.NativeType != "image" && column.NativeType != "timestamp")
			{
				if (column.Size > 0)
				{
					param = "(" + column.Size + ")";
				}
				else if (column.Size == -1)
				{
					param = "(MAX)";
				}
			}
			break;
		}
	}	
	return column.NativeType+param+(((bool)column.AllowDBNull)?" AllowDBNull":" NotAllowDBNull");
}
public string GetMemberVariableDeclarationStatement(ColumnSchema column)
{
	return GetMemberVariableDeclarationStatement("private", column);
}
public string GetMemberVariableDeclarationStatement(ViewColumnSchema column)
{
	return GetMemberVariableDeclarationStatement("private", column);
}

public string GetMemberVariableDeclarationStatement(string protectionLevel, ColumnSchema column)
{
	string statement = protectionLevel + " ";
	statement += GetCSharpVariableType(column) +GetNullableMark(column)+ " " + GetMemberVariableName(column);
	
	string defaultValue =null;
	defaultValue= GetMemberVariableDefaultValue(column);
	if(column.AllowDBNull==false)
	{
		
	}
	
	if (defaultValue != null&&defaultValue.Trim()!="")
	{
		statement += " = " + defaultValue;
	}
	
	statement += ";";
	
	return statement;
}
public string GetMemberVariableDeclarationStatement(string protectionLevel, ViewColumnSchema column)
{
	string statement = protectionLevel + " ";
	statement += GetCSharpVariableType(column) +GetNullableMark(column)+ " " + GetMemberVariableName(column);
	
	string defaultValue =null;
	defaultValue= GetMemberVariableDefaultValue(column);
	if(column.AllowDBNull==false)
	{
		
	}
	
	if (defaultValue != null&&defaultValue.Trim()!="")
	{
		statement += " = " + defaultValue;
	}
	
	statement += ";";
	
	return statement;
}

public string GetCamelCaseName(string value)
{
	if(value.ToLower()=="id") return "id";
	return value.Substring(0, 1).ToLower() + value.Substring(1);
}

public string GetMemberVariableName(ColumnSchema column)
{
	string propertyName = GetPropertyName(column);
	string memberVariableName = "_" + GetCamelCaseName(propertyName);
	
	return memberVariableName;
}
public string GetMemberVariableName(ViewColumnSchema column)
{
	string propertyName = GetPropertyName(column);
	string memberVariableName = "_" + GetCamelCaseName(propertyName);
	
	return memberVariableName;
}

public string GetPropertyName(ColumnSchema column)
{
	return column.Name;
	
}
public string GetPropertyName(ViewColumnSchema column)
{
	return column.Name;
}
		
public string GetMemberVariableDefaultValue(ColumnSchema column)
{
	string defaultValue = GetCSDefaultValueByType(column);
	if (defaultValue != null)
		return defaultValue;
	switch (column.DataType)
	{
		case DbType.Guid:
		{
			return "Guid.Empty";
		}
		case DbType.AnsiString:
		case DbType.AnsiStringFixedLength:
		case DbType.String:
		case DbType.StringFixedLength:
		{
			return "String.Empty";
		}
		default:
		{
			return "";
		}
	}
}
public string GetMemberVariableDefaultValue(ViewColumnSchema column)
{
	ColumnSchema tcolumn=GetRelateColumn(column);
	if(tcolumn==null)
	{
		return null;
	}
	return GetMemberVariableDefaultValue(tcolumn);
	
	
}
public ColumnSchema GetRelateColumn(ViewColumnSchema column)
{
	if(this.SourceTable.Columns.Contains(column.Name))
	{
		return this.SourceTable.Columns[column.Name];
	}
	return null;
}

public string GetCSharpVariableType(ColumnSchema column)
{		
	switch (column.DataType)
	{
		case DbType.AnsiString: return "string";
		case DbType.AnsiStringFixedLength: return "string";
		case DbType.Binary: return "byte[]";
		case DbType.Boolean: return "bool";
		case DbType.Byte: return "byte";
		case DbType.Currency: return "decimal";
		case DbType.Date: return "DateTime";
		case DbType.DateTime: return "DateTime";
		case DbType.Decimal: return "decimal";
		case DbType.Double: return "double";
		case DbType.Guid: return "Guid";
		//case DbType.Guid: return "string";
		case DbType.Int16: return "short";
		case DbType.Int32: return "int";
		case DbType.Int64: return "long";
		case DbType.Object: return "object";
		case DbType.SByte: return "sbyte";
		case DbType.Single: return "float";
		case DbType.String: return "string";
		case DbType.Xml: return "string";
		case DbType.StringFixedLength: return "string";
		case DbType.Time: return "TimeSpan";
		case DbType.UInt16: return "ushort";
		case DbType.UInt32: return "uint";
		case DbType.UInt64: return "ulong";
		case DbType.VarNumeric: return "decimal";
		default:
		    break;
	}
    if(column.NativeType=="character varying")
    {
        return "string";
    }
    if(column.NativeType=="integer")
    {
        return "int";
    }
    if(column.NativeType=="character")
    {
        return "string";
    }
    
    return "__UNKNOWN__"+column.NativeType;
}
public string GetCSharpVariableType(ViewColumnSchema column)
{	
	switch (column.DataType)
	{
		case DbType.AnsiString: return "string";
		case DbType.AnsiStringFixedLength: return "string";
		case DbType.Binary: return "byte[]";
		case DbType.Boolean: return "bool";
		case DbType.Byte: return "byte";
		case DbType.Currency: return "decimal";
		case DbType.Date: return "DateTime";
		case DbType.DateTime: return "DateTime";
		case DbType.Decimal: return "decimal";
		case DbType.Double: return "double";
		case DbType.Guid: return "Guid";
		//case DbType.Guid: return "string";
		case DbType.Int16: return "short";
		case DbType.Int32: return "int";
		case DbType.Int64: return "long";
		case DbType.Object: return "object";
		case DbType.SByte: return "sbyte";
		case DbType.Single: return "float";
		case DbType.String: return "string";
		case DbType.StringFixedLength: return "string";
		case DbType.Time: return "TimeSpan";
		case DbType.UInt16: return "ushort";
		case DbType.UInt32: return "uint";
		case DbType.UInt64: return "ulong";
		case DbType.VarNumeric: return "decimal";
		default:
		    break;
	}
     if(column.NativeType=="character varying")
    {
        return "string";
    }
    if(column.NativeType=="integer")
    {
        return "int";
    }
    if(column.NativeType=="character")
    {
        return "string";
    }
    return "__UNKNOWN__" + column.NativeType;
}


public string GetClassName(TableSchema table)
{
	if(!string.IsNullOrEmpty(ClassName))
	{
		return ClassName.Substring(0,1).ToUpper()+ClassName.Substring(1,ClassName.Length-1);
	}
	string className= table.Name;
	int index=className.LastIndexOf("_");
	if(index!=-1)
	{
		return className.Substring(index+1,className.Length-index-1);
	}
	return className.Substring(0,1).ToUpper()+className.Substring(1,className.Length-1);;
}

public string GetSqlDbType(ColumnSchema column)
{
	switch (column.NativeType)
	{
		case "bigint": return "BigInt";
		case "binary": return "Binary";
		case "bit": return "Bit";
		case "char": return "Char";
		case "datetime": return "DateTime";
		case "decimal": return "Numeric";
		case "float": return "Float";
		case "image": return "Image";
		case "int": return "Int";
		case "money": return "Money";
		case "nchar": return "NChar";
		case "ntext": return "NText";
		case "numeric": return "Numeric";
		case "nvarchar": return "NVarChar";
		case "real": return "Real";
		case "smalldatetime": return "SmallDateTime";
		case "smallint": return "SmallInt";
		case "smallmoney": return "SmallMoney";
		case "sql_variant": return "Variant";
		case "sysname": return "NChar";
		case "text": return "Text";
		case "timestamp": return "Timestamp";
		case "tinyint": return "TinyInt";
		case "uniqueidentifier": return "UniqueIdentifier";
		case "varbinary": return "VarBinary";
		case "varchar": return "VarChar";
		default: break;
	}
    if(column.NativeType=="character varying")
    {
        return "Varchar";
    }
    if(column.NativeType=="integer")
    {
        return "Integer";
    }
    if(column.NativeType=="date")
    {
        return "Date";
    }
    if(column.NativeType=="character")
    {
        return "Varchar";
    }
    if(column.NativeType=="timestamp without time zone")
    {
        return "Timestamp";
    }
    if(column.NativeType=="timestamp with time zone")
    {
        return "TimestampTZ";
    }
    return "__UNKNOWN__"+column.NativeType;
}

public string GetPrimaryKeyType(TableSchema table)
{
    return "Bool";
    //if (table.PrimaryKey != null)
    //{
    //    if (table.PrimaryKey.MemberColumns.Count == 1)
    //    {
    //        return GetCSharpVariableType(table.PrimaryKey.MemberColumns[0]);
    //    }
    //    else
    //    {
    //        throw new ApplicationException("This template will not work on primary keys with more than one member column.");
    //    }
    //}
    //else
    //{
    //    throw new ApplicationException("This template will only work on tables with a primary key.");
    //}
}
public string GetNullableMark(ColumnSchema column)
{
	string isNullable="";
	string type=GetCSharpVariableType(column);
	if(type!="string"&&type!="object")
	{
		if(column.AllowDBNull==true)
		{
			isNullable="?";
		}
	}
	return isNullable;
}
public string GetNullableMark(ViewColumnSchema column)
{
	string isNullable="";
	string type=GetCSharpVariableType(column);
	if(type!="string"&&type!="object")
	{
		if(column.AllowDBNull==true)
		{
			isNullable="?";
		}
	}
	return isNullable;
}
/// <summary>
/// This method returns the default value from the database if it is available.  It returns null
/// if no default value could be parsed.
/// 
/// NOTE: rudimentary support for default values with computations/functions is built in.  Right now th
///   only supported function is getdate().  Any others can be added below if desired.
/// </summary>
/// <param name="column"></param>
public string GetCSDefaultValueByType(DataObjectBase column)
{
	if (column == null)
		return null;

	ExtendedProperty defaultValueProperty = column.ExtendedProperties["CS_Default"];
	if (defaultValueProperty == null)
		return null;			
		
	string defaultValue = null;
	
	#region Convert declarations
	bool boolConvert;
	byte byteConvert;
	decimal decimalConvert;
	double doubleConvert;
	float floatConvert;
	int intConvert;
	long longConvert;
	short shortConvert;
	DateTime dateConvert;
	Guid guidConvert; 
	#endregion

	try
	{
		//Get Default Value 
		defaultValue = defaultValueProperty.Value.ToString();
		
		if (defaultValue == null || defaultValue.Trim().Length == 0)
			return null;
		
		// trim off the surrounding ()'s if they exist (SQL Server)
		while (defaultValue.StartsWith("(") && defaultValue.EndsWith(")") 
			|| defaultValue.StartsWith("\"") && defaultValue.EndsWith("\""))
		{
			defaultValue = defaultValue.Substring(1);
			defaultValue = defaultValue.Substring(0, defaultValue.Length - 1);
		}
		Debug.Write(column.Name+"---"+defaultValue);
		if (IsNumericType(column as ColumnSchema))
			defaultValue = defaultValue.TrimEnd('.');
			
		if (defaultValueProperty.DataType == DbType.String)
		{
			// probably a char type.  Let's remove the quotes so parsing is happy
			if (defaultValue.StartsWith("'") && defaultValue.EndsWith("'"))
			{
				defaultValue = defaultValue.Substring(1);
				defaultValue = defaultValue.Substring(0, defaultValue.Length - 1);
			}

			//this is probably a custom function, lets handle it sane-like
			if (defaultValue.Contains("()"))
			{
				if ( defaultValue.ToLower() == "getdate()" )
					defaultValue = "DateTime.Now";
				else if ( defaultValue.ToLower() == "newid()" )
					defaultValue = "new Guid()";
				else if ( defaultValue.ToLower() == "getutcdate()" )
					defaultValue = "DateTime.UtcNow";
				else
					return null;
			}
		}

		if (column.NativeType.ToLower() == "real")
		{
			floatConvert = float.Parse(defaultValue);
			if (defaultValue != null)
				return floatConvert.ToString() + "F";
			else
				return null;
		}
		else
		{
			DbType dataType = column.DataType;
			switch (dataType)
			{
				case DbType.AnsiString:				
				case DbType.AnsiStringFixedLength:
				case DbType.StringFixedLength:
				case DbType.String:
					if (defaultValue != null)
						return "\"" + defaultValue + "\"";
					else
						return null;
	
				case DbType.Boolean:
				
					if (defaultValue != null )
					{
						if (defaultValue == "1") return "true";
						if (defaultValue == "0") return "false";
						if (defaultValue.Trim().ToLower() == "yes") return "true";
						if (defaultValue.Trim().ToLower() == "no") return "false";
						if (defaultValue.Trim().ToLower() == "y") return "true";
						if (defaultValue.Trim().ToLower() == "n") return "false";
						
						boolConvert = bool.Parse(defaultValue);
						return boolConvert.ToString();
					}
					else
						return null;
	

	
				case DbType.Guid:
					if (defaultValue == "new Guid()")
						return defaultValue;
						
					guidConvert = new Guid(defaultValue);
					if (defaultValue != null && guidConvert != null)
						return "new Guid(\"" + guidConvert.ToString() + "\")";
					else
						return null;
				case DbType.Xml:
						return null;			
	
				//Answer modified was just 0
				case DbType.Binary:
					return null;
	
				//Answer modified was just 0
				case DbType.Byte:
					if (defaultValue != null )
					{
						byteConvert = byte.Parse(defaultValue);
						return "(byte)" + byteConvert.ToString();
					}
					else
						return null;
	
				case DbType.Currency:
					if (defaultValue != null)
					{
						decimalConvert = decimal.Parse(defaultValue);
						return decimalConvert.ToString() + "m";
					}
					else
						return null;
	
				case DbType.Date:
				case DbType.DateTime:
				
					if (defaultValue == "DateTime.Now")
						return "DateTime.Now";
					if (defaultValue == "DateTime.UtcNow")
						return "DateTime.UtcNow";

					dateConvert = DateTime.Parse(defaultValue);
					if (defaultValue != null )
						return "DateTime.Parse(\"" + dateConvert.ToString() + "\")";
			
					return null;
				
				case DbType.Decimal:
					if (defaultValue != null)
					{
						decimalConvert = decimal.Parse(defaultValue);
						return decimalConvert.ToString() + "m";
					}
					else
						return null;
	
				case DbType.Double:
					if (defaultValue != null)
					{
						floatConvert = float.Parse(defaultValue);
						return floatConvert.ToString() + "f";
					}
					else
						return null;
	
				case DbType.Int16:
					if (defaultValue != null)
					{
						shortConvert = short.Parse(defaultValue);
						return "(short)" + shortConvert.ToString();
					}
					else
						return null;
	
				case DbType.Int32:
					if (defaultValue != null )
					{
						intConvert = int.Parse(defaultValue);
						return "(int)" + intConvert.ToString();
					}
					else
						return null;
	
				case DbType.Int64:
					if (defaultValue != null)
					{
						longConvert = long.Parse(defaultValue);
						return "(long)" + longConvert.ToString();
					}
					else
						return null;
	
				case DbType.Object:
					return null;
	
				case DbType.Single:
					if (defaultValue != null)
					{
						floatConvert = float.Parse(defaultValue);
						return floatConvert.ToString() + "F";
					}
					else
						return null;
	
				case DbType.Time:
					if (defaultValue == "DateTime.Now")
						return defaultValue;
					else if (defaultValue != null)
					{
						dateConvert = DateTime.Parse(defaultValue);
						return "DateTime.Parse(\"" + dateConvert.ToString() + "\")";
					}
					return null;
				case DbType.VarNumeric:
					if (defaultValue != null)
					{	
						decimalConvert = decimal.Parse(defaultValue);
						return decimalConvert.ToString();
					}
					else
						return null;
				//the following won't be used
				//		case DbType.SByte: return "0";
				//		case DbType.UInt16: return "0";
				//		case DbType.UInt32: return "0";
				//		case DbType.UInt64: return "0";
				default: return null;
			}
		}
	}
	catch{}
	return null;
}
/// <summary>
/// Determines if the column is a numeric column or not.
/// </summary>
/// <param name="column">DB table column to be checked</param>
/// <returns>true when Numeric, otherwise false</returns>
public bool IsNumericType(ColumnSchema column)
{
	switch (column.NativeType.ToLower())
	{
		case "bigint":
		case "bit":
		case "decimal":
		case "float":
		case "int":
		case "money":
		case "numeric":
		case "real":
		case "smallint":
		case "smallmoney":
		case "tinyint": return true;
		default: return false;
	}
}